package com.eclink.asset.consume.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eclink.asset.consume.domain.dao.ConsumeInventoryCheck;
import com.eclink.asset.consume.domain.dao.ConsumeInventoryCheckRelItem;
import com.eclink.asset.consume.domain.query.ConsumeInventoryCheckInventoryQuery;
import com.eclink.asset.consume.mapper.ConsumeInventoryCheckMapper;
import com.eclink.asset.consume.mapper.ConsumeInventoryCheckRelItemMapper;

import com.eclink.asset.domain.query.AssetInventoryQuery;
import com.eclink.asset.enums.AssetInventoryItemResultEnum;
import com.eclink.asset.enums.AssetInventoryStateEnum;
import com.eclink.asset.service.CommonUserService;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.utils.SecurityUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 资产分类表Service业务层处理
 *
 *
 * @author zcc
 */
@Service
public class ConsumeInventoryCheckDetailService {

    @Resource
    private ConsumeInventoryCheckRelItemMapper mapper;

    @Resource
    private ConsumeInventoryCheckMapper consumeInventoryCheckMapper;

    @Autowired
    private CommonUserService commonUserService;

    /**
     * 查询资产盘点的清单
     *
     * @param query 查询条件
     * @param page 分页条件
     * @return 资产分类表
     */
    public List<ConsumeInventoryCheckRelItem> selectListDetailItem(AssetInventoryQuery query, Pageable page, String parentId) {
        if (page != null) {
            PageHelper.startPage(page.getPageNumber() + 1, page.getPageSize());
        }
        QueryWrapper<ConsumeInventoryCheckRelItem> qw = new QueryWrapper<>();
        qw.eq("del_flag",0);
        qw.eq("parent_id",parentId);

        String number = query.getNumber();
        if (!StringUtils.isEmpty(number)) {
            qw.like("number", number);
        }
        if (StringUtils.isNotBlank(query.getValue())) {
            qw.like(query.getField(), query.getValue());
        }
        return mapper.selectList(qw);
    }

    private BigDecimal getTotalPrice(Integer num, BigDecimal price) {
        return price.multiply(BigDecimal.valueOf(num));
    }

    /**
     * 盘点
     * @param query 盘点请求
     */
    @Transactional(rollbackFor = Exception.class)
    public void inventory(ConsumeInventoryCheckInventoryQuery query) {
        Long id = query.getId();
        Integer numReal = query.getNumReal();
        String remark = query.getRemark();
        ConsumeInventoryCheckRelItem item = mapper.selectById(id);
        Integer num = item.getNum();
        int compare = num.compareTo(numReal);
        if (compare == 0) {
            item.setState(AssetInventoryItemResultEnum.NORMAL.getValue());
        } else if (compare < 0) {
            item.setState(AssetInventoryItemResultEnum.OVER.getValue());
        } else {
            item.setState(AssetInventoryItemResultEnum.LOSS.getValue());
        }
        item.setNumReal(numReal);
        item.setTotalPriceReal(getTotalPrice(numReal, item.getPrice()));
        item.setDiff(numReal - num);
        item.setRemark(remark);
        Long userId = SecurityUtils.getUserId();
        item.setUserId(userId);
        item.setUserName(commonUserService.getNickNameById(userId));
        item.setInventoryTime(LocalDateTime.now());
        mapper.updateById(item);


    }



}
